/*
 * @Description: 短信发送日志模块
 * @Version: 2.0
 * @Author: Symbol_Yang
 * @Date: 2022-10-05 09:46:59
 * @LastEditors: Symbol_Yang
 * @LastEditTime: 2022-10-05 11:13:01
 */
const { ShortMsgLogModel, EnterpriseModel, CabinetModel, ShortMsgModel} = require('../../models/index')
const koaTool = require('../../commons/koa_tool')

class ShortMsgLogController {

    list = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, [])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let {
            page,
            limit,
            search,
        } = ctx.request.query;

        limit = Number(limit)
        page = page || 0
        page = page - 1
        let query = {  };

       
        let total = await ShortMsgLogModel.countDocuments(query)
        let list = await ShortMsgLogModel.find(query).sort([['create_at', -1]]).skip(page * limit).limit(limit).lean();

        let enterpriseList = await  EnterpriseModel.find().lean();
        let cabinetList = await CabinetModel.find().lean();
        let enterpriseMap = enterpriseList.reduce((a,b) => Object.assign(a, {[b._id]: b.name}), {});
        let cabinetMap = cabinetList.reduce((a,b) => Object.assign(a, {[b._id]: b.name}), {});
        let shortMsgTempList = await ShortMsgModel.find().lean();
        let shortMsgTempMap = shortMsgTempList.reduce((a,b) => Object.assign(a, {[b._id]: b.templateName}), {});
        const { SEND_STATUS } = require('../../commons/const.js');
        list.forEach(item => {
            item.sendStatusName = SEND_STATUS[item.send_status]
            item.enterpriseName = enterpriseMap[item.enterprise_id]
            item.shortMsgName = shortMsgTempMap[item.short_msg_id]
            item.cabinetName = cabinetMap[item.cabinet_id]
        })
        koaTool.succ(ctx, {
            list,
            total
        })
    }


}

module.exports = new ShortMsgLogController()
